package com.smartcruise.main.contactsFragment;

import com.smartcruise.base.RefreshContract;
import com.smartcruise.data.Contacts;
import com.smartcruise.util.ListUtils;
import com.smartcruise.util.network.callBack.SimpleCallBack;
import com.smartcruise.util.network.netUtils.NetCollection;
import com.smartcruise.util.network.netUtils.Result;
import com.smartcruise.util.sortLetter.CharacterParser;
import com.smartcruise.util.sortLetter.PinyinComparator;
import com.smartcruise.util.sortLetter.SortModel;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import io.realm.RealmChangeListener;
import io.realm.RealmResults;

/**
 * Created by FengChaoQun
 * on 2018/1/20 0020
 */

public class ContactsPresenterImp implements RefreshContract.Presenter {

    private RefreshContract.View mView;
    private RefreshContract.RefreshInterface refreshInterface;
    private List<SortModel<Contacts>> sortModels = new ArrayList<>();
    private CharacterParser characterParser = new CharacterParser();
    private PinyinComparator pinyinComparator = new PinyinComparator();
    private RealmResults<Contacts> contactsRealmResults;

    public ContactsPresenterImp(RefreshContract.View mView) {
        this.mView = mView;
        refreshInterface = mView.getRefresh();
    }

    @Override
    public void start() {
        contactsRealmResults = mView.getRealm().where(Contacts.class).findAll();
        contactsRealmResults.addChangeListener(new RealmChangeListener<RealmResults<Contacts>>() {
            @Override
            public void onChange(RealmResults<Contacts> results) {
                setData(contactsRealmResults);
            }
        });

        setData(contactsRealmResults);

        if (contactsRealmResults.isEmpty()) {
            refreshInterface.autoRefresh();
        }

        refreshInterface.setNoMoreData(true);
    }

    private void setData(RealmResults<Contacts> contactsRealmResults) {
        //先清空原来的数据
        sortModels.clear();
        if (contactsRealmResults != null && !contactsRealmResults.isEmpty()) {
            for (Contacts contactsRealmResult : contactsRealmResults) {
                SortModel<Contacts> sortModel = new SortModel<>();
                sortModel.setData(contactsRealmResult);
                if (contactsRealmResult.isEmergency()) {
                    sortModel.setRemark(1);
                }

                //汉字转换成拼音
                String pinyin = characterParser.getSelling(contactsRealmResult.getUserName());
                String sortString = pinyin.substring(0, 1).toUpperCase();
                // 正则表达式，判断首字母是否是英文字母
                if (sortString.matches("[A-Z]")) {
                    sortModel.setSortLetters(sortString.toUpperCase());
                } else {
                    sortModel.setSortLetters("#");
                }
                sortModels.add(sortModel);
            }
        }

        if (ListUtils.isHaveContent(sortModels)) {
            Collections.sort(sortModels, pinyinComparator);
        }
        ContactsAdapter mAdapter = new ContactsAdapter(mView.getActivity(), sortModels);
        mView.setData(mAdapter);
    }

    /**
     * description:下拉刷新
     */
    @Override
    public void refresh() {
        NetCollection.getContacts(mView, new SimpleCallBack(mView) {
            @Override
            public void onSuccess(Result result) {

            }
        });
    }

    /**
     * description:上拉加载
     */
    @Override
    public void loadMore() {

    }

    @Override
    public void destroy() {
        if (contactsRealmResults != null) {
            contactsRealmResults.removeAllChangeListeners();
        }
    }
}
